package com.example.asus.zyfypt_tea_307plx.activities;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.util.TypedValue;

import com.example.asus.zyfypt_tea_307plx.R;
import com.example.asus.zyfypt_tea_307plx.fragments.Fragment1;
import com.example.asus.zyfypt_tea_307plx.fragments.Fragment2;
import com.example.asus.zyfypt_tea_307plx.fragments.Fragment3;
import com.example.asus.zyfypt_tea_307plx.fragments.Fragment41;
import com.example.asus.zyfypt_tea_307plx.fragments.Fragment42;
import com.example.asus.zyfypt_tea_307plx.fragments.Fragment43;
import com.example.asus.zyfypt_tea_307plx.fragments.Fragment44;
import com.example.asus.zyfypt_tea_307plx.fragments.Fragment45;
import com.example.asus.zyfypt_tea_307plx.fragments.Fragment5;
import com.example.asus.zyfypt_tea_307plx.fragments.PagerSlidingTabStrip;


/**
 * Created by Cigarette on 2018/6/14.
 */

public class MyCollect extends AppCompatActivity {
    //获取当前屏幕的密度
    private DisplayMetrics dm;
    private PagerSlidingTabStrip pagersliding;
    private ViewPager viewpager;
    private Fragment41 fragment41 = null;
    private Fragment42 fragment42 = null;
    private Fragment43 fragment43 = null;
    private Fragment44 fragment44 = null;
    private Fragment45 fragment45 = null;
    private Fragment1 fragment1 = null;
    private Fragment2 fragment2 = null;
    private Fragment3 fragment3 = null;

    private Fragment5 fragment5 = null;

    public MyCollect() {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_collect);
        //System.out.println("--f3--" + getSessionId());
        dm = getResources().getDisplayMetrics();//获取屏幕密度
        viewpager = (ViewPager) findViewById(R.id.viewPager1);
        viewpager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
        pagersliding = (PagerSlidingTabStrip) findViewById(R.id.pagerslidingtabstrip1);
        pagersliding.setViewPager(viewpager);
        setpagerstyle();//设置PagerSlidingTabStrip显示效果
    }

//    //生命周期方法，View创建完成
//    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
//        //System.out.println("--f3--" + getSessionId());
//        dm = getResources().getDisplayMetrics();//获取屏幕密度
//        viewpager = (ViewPager) view.findViewById(R.id.viewPager);
//        viewpager.setAdapter(new MyCollect.MyPagerAdapter(getSupportFragmentManager()));
//        pagersliding = (PagerSlidingTabStrip) view.findViewById(R.id.pagerslidingtabstrip);
//        pagersliding.setViewPager(viewpager);
//        setpagerstyle();//设置PagerSlidingTabStrip显示效果
//    }

    private void setpagerstyle() {
        pagersliding.setShouldExpand(true); // 设置Tab是自动填充满屏幕的
        pagersliding.setDividerColor(Color.TRANSPARENT); // 设置Tab的分割线是透明的
        // 设置Tab底部线的高度
        pagersliding.setUnderlineHeight((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 1, dm));
        // 设置Tab Indicator的高度
        pagersliding.setIndicatorHeight((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 4, dm));
        // 设置Tab标题文字的大小
        pagersliding.setTextSize((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_SP, 16, dm));
        pagersliding.setIndicatorColor(Color.parseColor("#45c01a"));// 设置Tab Indicator的颜色
        // 设置选中Tab文字的颜色 (这是自定义的一个方法)
        pagersliding.setSelectedTextColor(Color.parseColor("#45c01a"));
        pagersliding.setTabBackground(0); // 取消点击Tab时的背景色
    }

    //自定义ViewPagerAdapter子类
    private class MyPagerAdapter extends FragmentPagerAdapter {
        private String[] titles = {"文章", "课件","视频","案例","项目"};//显示在二级导航上的标题文字
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }
        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];//确定当页导航上文字
        }
        @Override
        public int getCount() {
            return titles.length;//二级导航个数
        }
        @Override
        public Fragment getItem(int position) { //根据位置返回具体某个导航上对应的Fragment
            switch (position) {
                case 0:
                    if (fragment1 == null) {
                        fragment1 = new Fragment1();
                    }
                    return fragment1;
                case 1:
                    if (fragment2 == null) {
                        fragment2 = new Fragment2();
                    }
                    return fragment2;
                case 2:
                    if (fragment43 == null) {
                        fragment43 = new Fragment43();
                    }
                    return fragment43;
                case 3:
                    if (fragment44 == null) {
                        fragment44 = new Fragment44();
                    }
                    return fragment44;
                case 4:
                    if (fragment45 == null) {
                        fragment45 = new Fragment45();
                    }
                    return fragment45;
                default:
                    return null;
            }
        }
    }
}
